using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Text;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
using Server = Microsoft.SqlServer.Management.Smo.Server;
	internal class ScriptDatabaseRoleCommand : ScriptObjectCommand
	{
		private const string AddDatabaseRoleMember90 = "EXEC sys.sp_addrolemember @rolename=N'{0}', @membername=N'{1}'";

		private const string AddDatabaseRoleMember80 = "EXEC dbo.sp_addrolemember @rolename=N'{0}', @membername=N'{1}'";

		public ScriptDatabaseRoleCommand(Server server, ObjectScriptedHandler scriptTarget, ScriptActions scriptAction) : base(server, scriptTarget, scriptAction)
		{
		}

		protected override void ScriptCreate(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTargetHandler)
		{
			StringCollection stringCollection = new StringCollection();
			try
			{
				string arg_11_0 = ((DatabaseRole)smoObject).Owner;
			}
			catch
			{
			}
			ReflectionUtilities.InvokeMethod(smoObject, "ScriptCreateInternal", new object[]
			{
				stringCollection,
				options
			});
			StringCollection script = ScriptDatabaseRoleCommand.AddIfNotExistsForUser(options, stringCollection, (DatabaseRole)smoObject);
			scriptTargetHandler(new ObjectInfo(((DatabaseRole)smoObject).Name, smoObject.Urn), script, ScriptActions.Create);
		}

		protected override void ScriptPermission(SqlSmoObject smoObject, ScriptingOptions options, ObjectScriptedHandler scriptTarget)
		{
			StringCollection stringCollection = new StringCollection();
			ReflectionUtilities.InvokeMethod(smoObject, "AddScriptPermission", new object[]
			{
				stringCollection,
				options
			});
			DatabaseRole databaseRole = (DatabaseRole)smoObject;
			foreach (string current in databaseRole.EnumMembers())
			{
				string format = (options.TargetServerVersion == SqlServerVersion.Version90) ? "EXEC sys.sp_addrolemember @rolename=N'{0}', @membername=N'{1}'" : "EXEC dbo.sp_addrolemember @rolename=N'{0}', @membername=N'{1}'";
				stringCollection.Add(string.Format(CultureInfo.InvariantCulture, format, new object[]
				{
					ReflectionUtilities.EscapeString(databaseRole.Name, '\''),
					ReflectionUtilities.EscapeString(current, '\'')
				}));
			}
			StringCollection stringCollection2 = ScriptDatabaseRoleCommand.AddIfNotExistsForUser(options, stringCollection, databaseRole);
			if (stringCollection2.Count > 0)
			{
				scriptTarget(new ObjectInfo(databaseRole.Name, smoObject.Urn), stringCollection2, ScriptActions.Permission);
			}
		}

		private static StringCollection AddIfNotExistsForUser(ScriptingOptions options, StringCollection script, DatabaseRole role)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (string current in script)
			{
				stringBuilder.AppendLine(current);
			}
			if (string.IsNullOrEmpty(stringBuilder.ToString().Trim()))
			{
				return new StringCollection();
			}
			string format = (options.TargetServerVersion == SqlServerVersion.Version80) ? "IF NOT EXISTS (SELECT * FROM dbo.sysusers WHERE name = N'{0}')\r\nBEGIN\r\n{1}\r\nEND\r\n" : "IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'{0}')\r\nBEGIN\r\n{1}\r\nEND\r\n";
			return new StringCollection
			{
				string.Format(CultureInfo.InvariantCulture, format, new object[]
				{
					role.Name,
					stringBuilder.ToString()
				})
			};
		}
	}
}
